// JavaScript Document
function getTasks(clientid){
	var selectedclienttasks = tasks[clientid];
	
	$('#tasks').html('');
	if(selectedclienttasks){
		$('#tasks').append('<option>Select task</option>');
		$.each(selectedclienttasks, function(key,value){
			$('#tasks').append('<option value="'+value['client_id']+'_'+value['id']+'">'+value['title']+'</option>');
		});
	}else{
		$('#tasks').append('<option>No client selected</option>');
	}
	//console.log(selectedclienttasks);
}


function addTask(ids){
	var ids = ids.split('_');
	var client_id = ids[0];
	var task_id = ids[1];
	
	var client = clients[client_id];
	var column_id = 'client_'+client_id;
	
	//rearrange task with id as key
	var selectedclienttasks = tasks[client_id];
	var selectedtask = new Array();
	$.each(selectedclienttasks, function(key,value){
		if(value['id'] == task_id){
			selectedtask = value;
		}
	});
	
	console.log(client);
	
	if($('tr#task_'+task_id).length == 0){
	
		if($("#"+column_id).length == 0) {
		  $('.table tr:first').after('<tr class="column_'+column_id+' '+column_id+'" id="'+column_id+'">'+
									'<td colspan="9" class="client"><img src="../../img/cross.png" width="10" onClick="javascript:removeClient(\''+client_id+'\',\''+client[0]['title']+'\')"/>'+client[0]['title']+'</td>'+
								'</tr>'+
								'<tr class="column_'+column_id+' taskof_'+client_id+'" id="task_'+task_id+'">'+
									'<td class="task bullet"><img src="../../img/cross.png" width="10" onClick="javascript:removeTask(\''+client_id+'\',\''+task_id+'\',\''+selectedtask['title']+'\')"/>'+selectedtask['title']+
										'<textarea name="data[content]['+client_id+']['+task_id+'][description]" class="textarea" placeholder="brief description"></textarea></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Monday']+' onchange="calculate(this,\'client_'+task_id+'\',\'mon\')" name="data[content]['+client_id+']['+task_id+'][mon]" class="textfieldsmall client_'+task_id+' mon" id="client_'+task_id+'_mon" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Tuesday']+' onchange="calculate(this,\'client_'+task_id+'\',\'tue\')" name="data[content]['+client_id+']['+task_id+'][tue]" class="textfieldsmall client_'+task_id+' tue" id="client_'+task_id+'_tue" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Wednesday']+' onchange="calculate(this,\'client_'+task_id+'\',\'wed\')" name="data[content]['+client_id+']['+task_id+'][wed]" class="textfieldsmall client_'+task_id+' wed" id="client_'+task_id+'_wed" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Thursday']+' onchange="calculate(this,\'client_'+task_id+'\',\'thur\')" name="data[content]['+client_id+']['+task_id+'][thur]" class="textfieldsmall client_'+task_id+' thur" id="client_'+task_id+'_thur" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Friday']+' onchange="calculate(this,\'client_'+task_id+'\',\'fri\')" name="data[content]['+client_id+']['+task_id+'][fri]" class="textfieldsmall client_'+task_id+' fri" id="client_'+task_id+'_fri" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Saturday']+' onchange="calculate(this,\'client_'+task_id+'\',\'sat\')" name="data[content]['+client_id+']['+task_id+'][sat]" class="textfieldsmall client_'+task_id+' sat" id="client_'+task_id+'_sat" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Sunday']+' onchange="calculate(this,\'client_'+task_id+'\',\'sun\')" name="data[content]['+client_id+']['+task_id+'][sun]" class="textfieldsmall client_'+task_id+' sun" id="client_'+task_id+'_sun" value="0"></td>'+
									'<td class="total">'+
										'<input type="text" readonly="" name="data[content]['+client_id+']['+task_id+'][total]" class="textfieldsmall" id="client_'+task_id+'_total" value="0"></td>'+
								'</tr>');
		}else{
			$('.table tr#'+column_id).after('<tr class="column_'+column_id+' taskof_'+client_id+'" id="task_'+task_id+'">'+
									'<td class="task bullet"><img src="../../img/cross.png" width="10" onClick="javascript:removeTask(\''+client_id+'\',\''+task_id+'\',\''+selectedtask['title']+'\')"/>'+selectedtask['title']+
										'<textarea name="data[content]['+client_id+']['+task_id+'][description]" class="textarea" placeholder="brief description"></textarea></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Monday']+' onchange="calculate(this,\'client_'+task_id+'\',\'mon\')" name="data[content]['+client_id+']['+task_id+'][mon]" class="textfieldsmall client_'+task_id+' mon" id="client_'+task_id+'_mon" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Tuesday']+' onchange="calculate(this,\'client_'+task_id+'\',\'tue\')" name="data[content]['+client_id+']['+task_id+'][tue]" class="textfieldsmall client_'+task_id+' tue" id="client_'+task_id+'_tue" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Wednesday']+' onchange="calculate(this,\'client_'+task_id+'\',\'wed\')" name="data[content]['+client_id+']['+task_id+'][wed]" class="textfieldsmall client_'+task_id+' wed" id="client_'+task_id+'_wed" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Thursday']+' onchange="calculate(this,\'client_'+task_id+'\',\'thur\')" name="data[content]['+client_id+']['+task_id+'][thur]" class="textfieldsmall client_'+task_id+' thur" id="client_'+task_id+'_thur" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Friday']+' onchange="calculate(this,\'client_'+task_id+'\',\'fri\')" name="data[content]['+client_id+']['+task_id+'][fri]" class="textfieldsmall client_'+task_id+' fri" id="client_'+task_id+'_fri" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Saturday']+' onchange="calculate(this,\'client_'+task_id+'\',\'sat\')" name="data[content]['+client_id+']['+task_id+'][sat]" class="textfieldsmall client_'+task_id+' sat" id="client_'+task_id+'_sat" value="0"></td>'+
									'<td class="hours">'+
										'<input type="text" '+holidaysthisweek['Sunday']+' onchange="calculate(this,\'client_'+task_id+'\',\'sun\')" name="data[content]['+client_id+']['+task_id+'][sun]" class="textfieldsmall client_'+task_id+' sun" id="client_'+task_id+'_sun" value="0"></td>'+
									'<td class="total">'+
										'<input type="text" readonly="" name="data[content]['+client_id+']['+task_id+'][total]" class="textfieldsmall" id="client_'+task_id+'_total" value="0"></td>'+
								'</tr>');
			
		}
	}
	else{
		alert('This task already exists in your timesheet');
	}
}



function removeClient(clientid,clientname){
	var confirmdelete = window.confirm("Are you sure you want to delete this client plus all its tasks?");
	if(confirmdelete == true){
		$('#add_'+clientid).attr('href','javascript:addClient("'+clientid+'","'+clientname+'")');
		$('#add_'+clientid).html('<img src="images/add.png" width="15"/>');
		
		//return child to add status
		$('a.childof'+clientid).each(function(){
			var taskid = $(this).attr('id').replace( /^\D+/g, '');
			var taskname = $(this).text();
			$(this).attr('href','javascript:addTask("'+clientid+'","'+taskid+'","'+taskname+'")');
			$(this).html('<img src="images/add.png" width="15"/>');
		});
		
		$('tr.column_client_'+clientid+' > td > input').each(function(){
			var removedID = $(this).attr('id');
			var removedDay = removedID.split('_');
			removedDay = removedDay[2];
			var removedValue = document.getElementById(removedID).value;
			console.log(removedID);
			
			//subtract the respective totals
			var currenttotal = document.getElementById('total_'+removedDay).value;
			var newtotal = parseInt(currenttotal) - parseInt(removedValue);
			document.getElementById('total_'+removedDay).value = newtotal;
		});
		
		$('tr.column_client_'+clientid).remove();
		if(tdclass=='even'){ tdclass = 'odd'; } else { tdclass = 'even'; }
	}
}


function removeTask(clientid,taskid,taskname){
	var confirmdelete = window.confirm("Are you sure you want to delete this tasks?");
	if(confirmdelete == true){
		var task_id = 'task_'+taskid;
		$('#add_task'+taskid).attr('href','javascript:addTask("'+clientid+'","'+taskid+'","'+taskname+'")');
		$('#add_task'+taskid).html('<img src="images/add.png" width="15"/>');
		
		$('tr#'+task_id+' > td > input').each(function(){
			var removedID = $(this).attr('id');
			var removedDay = removedID.split('_');
			removedDay = removedDay[2];
			var removedValue = document.getElementById(removedID).value;
			
			//subtract the respective totals
			var currenttotal = document.getElementById('total_'+removedDay).value;
			var newtotal = parseInt(currenttotal) - parseInt(removedValue);
			document.getElementById('total_'+removedDay).value = newtotal;
		});
		
		console.log($('.taskof_'+clientid).length);
		
		//if task is only one remove task and client rows(all)
		if($('.taskof_'+clientid).length == 1){
			$('tr.column_client_'+clientid).remove();
		}else{
			$('tr#'+task_id).remove();
		}
		
		if(tdclass=='even'){ tdclass = 'odd'; } else { tdclass = 'even'; }
	}
}



function calculate(thisfield,classrow,classcolumn){
	console.log(thisfield.value);
	if(isNaN(thisfield.value) || thisfield.value==""){
			alert('Enter numbers only');
			thisfield.value = 0;
			thisfield.focus();
	}
	
	//column/day total
	var daytotal = 0;
	$('.'+classcolumn).each(function(){
		var fieldid = $(this).attr('id');
		var fieldvalue = document.getElementById(fieldid).value;
		if(fieldvalue==""){ fieldvalue = 0; }
		daytotal = Number(daytotal) + Number(fieldvalue);
		console.log(daytotal)
	});
		
	//row/client total
	var clienttotal = 0;
	$('.'+classrow).each(function(){
		var fieldid = $(this).attr('id');
		var fieldvalue = document.getElementById(fieldid).value;
		if(fieldvalue==""){ fieldvalue = 0; }
		clienttotal = Number(clienttotal) + Number(fieldvalue);
		//console.log(Number(fieldvalue));
	});
	
	if(daytotal.toFixed(2) > 24){
		alert('The total hours for this day is more than the maximum 24hrs');
		thisfield.value = 0;
	}else{
		document.getElementById('total_'+classcolumn).value = daytotal.toFixed(2);
		document.getElementById(classrow+'_total').value = clienttotal.toFixed(2);	
	}
	

	
	
	
	//week total
	var montotal = document.getElementById('total_mon').value;
	var tuetotal = document.getElementById('total_tue').value;
	var wedtotal = document.getElementById('total_wed').value;
	var thurtotal = document.getElementById('total_thur').value;
	var fritotal = document.getElementById('total_fri').value;
	var sattotal = document.getElementById('total_sat').value;
	var suntotal = document.getElementById('total_sun').value;
	
	var totaltotal = Number(montotal) + Number(tuetotal) + Number(wedtotal) + Number(thurtotal) + Number(fritotal) + Number(sattotal) + Number(suntotal);
	document.getElementById('total_total').value = totaltotal.toFixed(2);//round it to 2 decimal points	
	/*console.log('mon = '+montotal);
	console.log('tue = '+tuetotal);
	console.log('wed = '+wedtotal);
	console.log('thur = '+thurtotal);
	console.log('fri = '+fritotal);
	console.log('sat = '+sattotal);
	console.log('sun = '+suntotal);*/
}

//onleave section for update timesheet
$('.leave_day').each(function(){
	var disabledclass = $(this).attr('name');
	$('.'+disabledclass).attr('readonly','readonly');
});

//select box
$('.selectbox').click(function(){
	$('ul.clientlist').slideToggle();
});




//add accordion
$('ul.clientlist li').each(function(){
	if($(this).find('ul.second_level').length == 1){
		$(this).prepend('<a href="javascript:void()" class="accordion colapsed"><img src="images/arrow-right.png" /></a>');
	}else{
		$(this).css('padding-left','40px');
	}
});

//accordion
$('.accordion').click(function(){
	var accordion = $(this);
	var submenu = $(this).nextAll('ul.second_level').first();
	
	submenu.slideToggle(500,function(){
		if(submenu.is(':visible')){
			accordion.find('img').attr('src','images/arrow-down.png');
		}
		
		if(submenu.is(':hidden')){
			accordion.find('img').attr('src','images/arrow-right.png');
		}
	});		
});

function validateForm(){
	if(document.getElementById('total_total').value == 0){
		alert('Your cannot save a timesheet with a total of 0');
		return false;
	}	
}


$('.removeleave').click(function(){
	console.log('am here');
	$(this).parents('tr').remove();
});


function addLeave(){
	var d = new Date();
	var now = (d.getTime()/1000) . toFixed(0);
	$('#leavedays').prepend('<tr>'+
    		'<td>'+
				'<label>End Date</label>'+
                '<input name="data[leavedays]['+now+'][startdate]" type="text" class="startdate" readonly="readonly" placeholder="select date">'+
			'<td>'+
				'<label>End Date</label>'+
                '<input name="data[leavedays]['+now+'][enddate]" type="text" class="enddate" readonly="readonly" placeholder="select date">'+
            '<td><a href="javascript:void(0)" class="removeleave">remove</a></td>'+
        '</tr>');
}



